package com.cn.zengzeng.spbzengzeng.test;

import com.cn.zengzeng.spbzengzeng.utils.LocalThreadPoolTaskExecutor;
import com.cn.zengzeng.spbzengzeng.utils.ThreadPoolExecutorFactory;
import kong.unirest.Headers;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.FileWriter;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * @Author: ZhangZengZeng
 * @Date: 2022/8/25 11:12
 */
@Slf4j
public class SsuccessiveDemo {

    private static final String URL = "https://caoliu1024.com/register.php";
    /**
     * 匹配6位顺增或顺降
     */
    private static final Pattern PATTERN_SIX = Pattern.compile("(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4" +
            "(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}|(?:9(?=8)" + "|8(?=7)" + "|7(?=6)|6(?=5)|5" +
            "(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){5})\\d");
    /**
     * 匹配4-9位连续的数字
     */
    private static final Pattern PATTERN_FOUR_NINE = Pattern.compile("(?:(?:0(?=1)|1(?=2)|2(?=3)|3(" +
            "?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){3,}|" + "(?:9" + "(?=8)|8(?=7)|7(?=6)|6(?=5)" +
            "|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3,})\\d");
    /**
     * 匹配3位以上的重复数字
     */
    private static final Pattern REPEATING_NUMBERS = Pattern.compile("([\\d])\\1{2,}");
    /**
     * 匹配2233类型
     */
    private static final Pattern TYPE_2233 = Pattern.compile("([\\d])\\1{1,}([\\d])\\2{1,}");


    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder();
        LocalThreadPoolTaskExecutor threadPoolExecutor = ThreadPoolExecutorFactory.get(10000000);
        final int count = 20001;
        int autoNumber = 20000;
        for (int i = autoNumber; i <count ; i++) {
            int finalI = i;
            threadPoolExecutor.execute(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    String userId = String.valueOf(finalI);
                    HttpResponse<String> stringHttpResponse = queryAccount(userId);
                    String cookie = getCookie(stringHttpResponse);
                    boolean b = checkRegist(stringHttpResponse);
                    log.info("Register:{}:flag:{}",userId,b);
                    if(b){
                        String password = userId+".com";
                        String email = getEmail();
                        boolean register = register(userId,password,email,cookie);
                        log.info("Register:{}:注册结果:{}",userId,register);
//                        if(register || !register) {
                            builder.append(userId).append(",").append(password).append(",")
                                    .append(email).append("\n");
//                        }
                    }
                }
            });
        }
//        isCompleted(threadPoolExecutor);
        writFile(builder.toString());
    }

    public static boolean getMatcher(String userId) {
        Matcher matcher = PATTERN_SIX.matcher(userId);
        Matcher matcher1 = PATTERN_FOUR_NINE.matcher(userId);
        Matcher matcher2 = REPEATING_NUMBERS.matcher(userId);
        Matcher matcher3 = TYPE_2233.matcher(userId);
        return matcher.matches() || matcher1.matches() || matcher2.matches() || matcher3.matches();
    }

    /**
    * @author ZhangZengZeng
    * @Description 判断任务是否执行完
    * @date 2022/8/25 23:16
    * @param threadPool 任务
    */
    private static void isCompleted(ThreadPoolExecutor threadPool) {
        threadPool.shutdown();
        while (true) {
            // 如果没有执行完就一直循环
            if(threadPool.isTerminated()){
                log.info("所有的子线程都结束了！");
                break;
            }
        }
    }
    /**
    * @author ZhangZengZeng
    * @Description 检测是否可以注册
    * @date 2022/8/26 9:23
    * @param stringHttpResponse  请求结果
    * @return {@link boolean} 判断结果
    */
    private static boolean checkRegist(HttpResponse<String> stringHttpResponse) {
        String body = stringHttpResponse.getBody();
        return body.contains("parent.retmsg('4')");
    }
    /**
    * @author ZhangZengZeng
    * @Description 获取校验cookie
    * @date 2022/8/26 9:23
    * @param stringHttpResponse 校验请求结果
    * @return {@link String} 返回cookie
    */
    public static String getCookie (HttpResponse<String> stringHttpResponse) {
        Headers headers = stringHttpResponse.getHeaders();
        List<String> strings = headers.get("Set-Cookie");
        log.info("cookie:{}",strings);
        String cookies = "";
        for (String string : strings) {
            if (string.contains("a2640_lastvisit")) {
                cookies = string;
            }
        }
        return cookies;
    }

    /**
    * @author ZhangZengZeng
    * @Description 查询是否已经注册
    * @date 2022/8/25 19:35
    * @param userId  用户ID
    * @return {@link boolean}
    */
    public static HttpResponse<String>  queryAccount(String userId) throws UnirestException {
        log.info("查询userID是否已注册:{}",userId);
        return Unirest.post(URL)
                .header("authority", "caoliu1024.com")
                .header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;" +
                        "q=0.8,application/signed-exchange;v=b3;q=0.9")
                .header("accept-language", "zh-CN,zh;q=0.9")
                .header("cache-control", "max-age=0")
                .header("content-type", "application/x-www-form-urlencoded")
                .header("cookie", "a2640_ol_offset=62481; a2640_online=no; a2640_lastpos=other; _gid=GA1.2.352613172.1661425619;" +
                        " _ga_QTT9CLMQSW=GS1.1.1661425618.1.1.1661425643.0.0.0; _ga=GA1.2.1705728788.1661425619;" +
                        " a2640_lastvisit=39%091661425656%09%2Fregister.php%3F; a2640_lastvisit=699%091661426316%09%2Fregister.php%3F")
                .header("origin", "https://caoliu1024.com")
                .header("referer", URL)
                .header("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("sec-fetch-dest", "iframe")
                .header("sec-fetch-mode", "navigate")
                .header("sec-fetch-site", "same-origin")
                .header("sec-fetch-user", "?1")
                .header("upgrade-insecure-requests", "1")
                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
                        "Chrome/104.0.0.0 Safari/537.36")
                .field("username", userId)
                .field("validate", "")
                .field("action", "regnameck")
                .asString();
    }
    /**
    * @author ZhangZengZeng
    * @Description 会员注册
    * @date 2022/8/25 19:40
    * @param username 用户名
    * @param password 用户密码
    * @param email 邮箱
    * @return {@link boolean}
    */
    public static boolean register(String username, String password,String email,String cookie) throws UnirestException {
        log.info("注册参数:username={} password={} email={}",username,password,email);
        HttpResponse<String> response = Unirest.post(URL)
                .header("authority", "caoliu1024.com")
                .header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng," +
                        "*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                .header("accept-language", "zh-CN,zh;q=0.9")
                .header("cache-control", "max-age=0")
                .header("content-type", "application/x-www-form-urlencoded")
                .header("cookie", cookie)
                .header("origin", "https://caoliu1024.com")
                .header("referer", URL+"?id=6068845")
                .header("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("sec-fetch-dest", "document")
                .header("sec-fetch-mode", "navigate")
                .header("sec-fetch-site", "same-origin")
                .header("sec-fetch-user", "?1")
                .header("upgrade-insecure-requests", "1")
                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" +
                        " Chrome/104.0.0.0 Safari/537.36")
                .field("id", "6068845")
                .field("regname", username)
                .field("regpwd", password)
                .field("regpwdrepeat", password)
                .field("regemail", email)
                .field("regsex", "0")
                .field("regbirthyear", "")
                .field("regbirthmonth", "")
                .field("regbirthday", "")
                .field("regoicq", "")
                .field("regfrom", "")
                .field("regicon", "1.png")
                .field("regsign", "")
                .field("regintroduce", "")
                .field("regifemail", "1")
                .field("forward", "")
                .field("step", "2")
                .asString();
        String body = response.getBody();
        return body.contains("完成註冊现在可以开始使用您的會員权利了");
    }
    /**
    * @author ZhangZengZeng
    * @Description 邀请注册
    * @date 2022/9/2 21:27
    * @param username 用户名
    * @param password 用户密码
    * @param email 邮箱
    * @param cookie cookie
    */
    public static void inviteRegister(String username, String password,String email,String cookie) throws UnirestException {
        HttpResponse<String> response = Unirest.post("https://caoliu1024.com/register.php")
                .header("authority", "caoliu1024.com")
                .header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                .header("accept-language", "zh-CN,zh;q=0.9")
                .header("cache-control", "max-age=0")
                .header("content-type", "application/x-www-form-urlencoded")
                .header("cookie", cookie)
                .header("origin", "https://caoliu1024.com")
                .header("referer", "https://caoliu1024.com/register.php?id=6068845")
                .header("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Microsoft Edge\";v=\"104\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("sec-fetch-dest", "document")
                .header("sec-fetch-mode", "navigate")
                .header("sec-fetch-site", "same-origin")
                .header("sec-fetch-user", "?1")
                .header("upgrade-insecure-requests", "1")
                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70")
                .field("id", "6068845")
                .field("regname", username)
                .field("regpwd", password)
                .field("regpwdrepeat", password)
                .field("regemail", email)
                .field("regsex", "0")
                .field("regbirthyear", "")
                .field("regbirthmonth", "")
                .field("regbirthday", "")
                .field("regoicq", "")
                .field("regfrom", "")
                .field("regicon", "1.png")
                .field("regsign", "")
                .field("regintroduce", "")
                .field("regifemail", "1")
                .field("forward", "")
                .field("step", "2")
                .asString();
    }
    /**
     * @author ZhangZengZeng
     * @Description 查询是否已经注册
     * @date 2022/8/25 19:35
     * @param userId  用户ID
     */
    public static HttpResponse<String>  inviteCheck(String userId) throws UnirestException {
        return Unirest.post("https://caoliu1024.com/register.php")
                .header("authority", "caoliu1024.com")
                .header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                .header("accept-language", "zh-CN,zh;q=0.9")
                .header("cache-control", "max-age=0")
                .header("content-type", "application/x-www-form-urlencoded")
                .header("cookie", "_ga=GA1.1.819668941.1662120939; spread_id=6068845; a2640_online=no; a2640_ol_offset=87410; a2640_lastpos=other; a2640_lastvisit=547%091662123823%09%2Fregister.php%3F; _ga_QTT9CLMQSW=GS1.1.1662123142.2.1.1662125382.0.0.0; a2640_lastvisit=2473%091662125749%09%2Fregister.php%3F; spread_id=6068845")
                .header("origin", "https://caoliu1024.com")
                .header("referer", "https://caoliu1024.com/register.php?id=6068845")
                .header("sec-ch-ua", "\"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Microsoft Edge\";v=\"104\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("sec-fetch-dest", "iframe")
                .header("sec-fetch-mode", "navigate")
                .header("sec-fetch-site", "same-origin")
                .header("sec-fetch-user", "?1")
                .header("upgrade-insecure-requests", "1")
                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70")
                .field("username", userId)
                .field("validate", "")
                .field("action", "regnameck")
                .asString();
    }
    /**
    * @author ZhangZengZeng
    * @Description 随机获取email参数
    * @date 2022/8/26 9:24
    * @return {@link String} 返回随机email
    */
    public static String getEmail() throws NoSuchAlgorithmException {
        Random rand = SecureRandom.getInstanceStrong();
        String []email = {"cliuyyds@gmail.com","xjsp6985@gmail.com","a13619206145@gmail.com",
                "2240495897@qq.com","2879931880@qq.com","1022907736@qq.com","stx136@foxmail.com",
                "lu1031132178@163.com","1031132178@qq.com","vvip0012022@163.com","vvip010@163.com",
                "cvip011@163.com","tbf9874@163.com"};
        int length = email.length;
        int num = rand.nextInt(length);
        return email[num];
    }
    /**
    * @author ZhangZengZeng
    * @Description 账号写入文件
    * @date 2022/8/26 9:24
    * @param text  写入内容
    */
    public static void writFile(String text) {
        try (FileWriter fw = new FileWriter("D:\\data\\registerAccount.txt")
         ) {
            fw.write(text);
            fw.flush();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
